<?php
class AjaxUserInformer_AdminSettingsController extends Core_Controller_Action_Admin {
	
	protected $_fieldType;
	
	protected $_requireProfileType = true;
	
	protected $_moduleName;
	
	public function init() {
		if (! $this->_moduleName) {
			$this->_moduleName = 'User';
		}
		
		if (! $this->_fieldType) {
			$this->_fieldType = Engine_Api::deflect ( $this->_moduleName );
		}
		
		$this->view->fieldType = $this->_fieldType;
	}
	
	public function indexAction() {
		$fieldType = Engine_Api::deflect ( 'User' );
		
		$mapData = Engine_Api::_ ()->getApi ( 'core', 'fields' )->getFieldsMaps ( $fieldType );
		$metaData = Engine_Api::_ ()->getApi ( 'core', 'fields' )->getFieldsMeta ( $fieldType );
		$optionsData = Engine_Api::_ ()->getApi ( 'core', 'fields' )->getFieldsOptions ( $fieldType );
		
		$topLevelMaps = $mapData->getRowsMatching ( array ('field_id' => 0, 'option_id' => 0 ) );
		
		$options = array ();
		
		foreach ( $topLevelMaps as $map ) {
			foreach ( $optionsData->getRowsMatching ( 'field_id', $map->getChild ()->field_id ) as $option ) {
				foreach ( $mapData->getRowsMatching ( 'option_id', $option->option_id ) as $secondLevelMap ) {
					$meta = $secondLevelMap->getChild ();
					$translate = Zend_Registry::get ( 'Zend_Translate' );
					
					if ($meta->type != 'heading')
						$options [$option->label] [$option->option_id . "_" . $meta->field_id] = $this->view->translate ( $meta->label );
				}
			}
		}
		
		$form = $this->view->form = new AjaxUserInformer_Form_Admin_General ();
		$form->end ( $options );
		
		$settings = Engine_Api::_ ()->getApi ( 'settings', 'MsepCore' );
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			$values = Engine_Api::_ ()->getApi ( 'core', 'MsepCore' )->cleanData ( $form->getValues (), 'ajaxui' );
			
			$user_fields = array ();
			
			foreach ( $values as $key => $value ) {
				if (substr ( $key, 0, 18 ) == 'ajaxui_user_fields') {
					unset ( $values [$key] );
					
					foreach ( $value as $v ) {
						$user_fields [] = $v;
					}
				}
			}
			
			$values ['ajaxui_user_fields'] = json_encode ( $user_fields );
			
			$settings->updateFromArray ( Engine_Api::_ ()->getDbtable ( 'settings', 'MsepCore' ), $values );
			
			$form->addNotice ( 'Changes Saved.' );
		}
		
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'ajaxui_admin_main', array (), 'ajaxui_admin_main_settings' );
	}
	
	public function visualAction() {
		Engine_Api::_ ()->getApi ( 'core', 'MsepCore' )->jsColor ();
		Engine_Api::_ ()->getApi ( 'core', 'MsepCore' )->addStyleFile ( 'application/modules/MsepCore/externals/styles/main.css' );
		Engine_Api::_ ()->getApi ( 'core', 'MsepCore' )->addStyleFile ( 'application/modules/MsepCore/externals/styles/admin/profile_fields.css' );
		
		$form = $this->view->form = new AjaxUserInformer_Form_Admin_Visual ();
		
		$this->view->settings = $settings = Engine_Api::_ ()->getApi ( 'settings', 'MsepCore' );
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			$values = Engine_Api::_ ()->getApi ( 'core', 'MsepCore' )->cleanData ( $form->getValues (), 'ajaxui' );
			
			$settings->updateFromArray ( Engine_Api::_ ()->getDbtable ( 'settings', 'MsepCore' ), $values );
			
			$form->addNotice ( 'Changes Saved.' );
		}
		
		$this->view->settings = Engine_Api::_ ()->getApi ( 'settings', 'MsepCore' );
		
		$this->view->fields = array ('background_color', 'border_color', 'border_width', 'corner_tl_radius', 'corner_tr_radius', 'corner_bl_radius', 'corner_br_radius' );
		
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'ajaxui_admin_main', array (), 'ajaxui_admin_main_visual' );
	}

}